﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MonoPhysics
{
    public class SimplifiedCircularLinkedListNode<T>
    {
        public T value;
        public SimplifiedCircularLinkedListNode<T> Next;
        public SimplifiedCircularLinkedListNode<T> Parent;

        public SimplifiedCircularLinkedListNode(T value)
        {
            this.value = value;
            this.Next = this;
            this.Parent = this;
        }

        /// <summary>
        /// Insere un element apres celui-ci.
        /// </summary>
        /// <param name="s">Noeud a inserer.</param>
        public void InsertAfter(SimplifiedCircularLinkedListNode<T> s)
        {
            s.Next = this.Next;
            s.Parent = this;
            this.Next.Parent = s;
            this.Next = s;
        }
        public void InsertAfter(T e)
        {
            SimplifiedCircularLinkedListNode<T> s = new SimplifiedCircularLinkedListNode<T>(e);
            s.Next = this.Next;
            s.Parent = this;
            this.Next.Parent = s;
            this.Next = s;
        }

        public void DeleteNext()
        {
            SimplifiedCircularLinkedListNode<T> nnext = this.Next;
            this.Next = nnext.Next;
            nnext.Next.Parent = this;
            nnext.Next = null;
        }

        public static void Swap(SimplifiedCircularLinkedListNode<T> s1, SimplifiedCircularLinkedListNode<T> s2)
        {
            SimplifiedCircularLinkedListNode<T> tmp;
            s1.Parent.Next = s2;
            s1.Next.Parent = s2;
            s2.Parent.Next = s1;
            s2.Next.Parent = s1;

            tmp = s1.Next;
            s1.Next = s2.Next;
            s2.Next = tmp;
            tmp = s1.Parent;
            s1.Parent = s2.Parent;
            s2.Parent = tmp;
        }
    }
}
